<?php
// +----------------------------------------------------------------------+
// | The CompanyName 星座女神                                             |
// +----------------------------------------------------------------------+
// | Copyright (c) 2016, CompanyName 星座女神. All rights reserved.       |
// +----------------------------------------------------------------------+
// | Authors: CompanyName 星座女神.                                       |
// |                                                                      |
// +----------------------------------------------------------------------+
/**
 * @version  2.2
 * @author   guokunsheng
 * @date     2017-07-012
 */
namespace App\Http\Controllers\Api\v1\Astro;
use App\Http\Controllers\Controller;
// 星盘记分类
class HousescoreController extends Controller
{
    private $base_data = [];
    private $base_active = [];
    // 12星座默认得分为0
    private $active_score = [1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0,];
    private $burn_score = [1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0,];
    private $light_score = [1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0,];
    private $house_name = [
        1=>'生命之宫',
        2=>'价值之宫',
        3=>'出入之宫',
        4=>'本源之宫',
        5=>'欢乐之宫',
        6=>'劳务之宫',
        7=>'合作之宫',
        8=>'生命之宫',
        9=>'迁移之宫',
        10=>'事业之宫',
        11=>'团体之宫',
        12=>'隐秘之宫',
    ];
    private $active = [];
    private $burn = [];
    private $lucky = [];

    private $high = 350;
    private $mid = 100;


    public function __construct($data = [])
    {
        if (empty($data)) return ['status' => '40001', 'Invalid Data!'];

        $this->base_data = $data;
        $Planetpower = new PlanetpowerController($data);
        $this->base_active = $Planetpower->_initload();
    }

    public function _initload()
    {
//        p($this->base_active);
        $this->get_house_active();
        $this->get_house_hurt();
        $this->get_house_light();
        $ret = [];
        for($i = 1;$i < 13;$i++)
        {
            $p = explode('|',config('astrolog.house_name')[$i]);
            $active = $this->active_score[$i];
            $lucky = $this->light_score[$i];
            $burn = $this->burn_score[$i];
            // 活跃值：输出值=x/800*500（若结果大于500，令其=500）
            $active = $active / 800 * 500;
            // 光明值：输出值=x*2（若结果大于500，令其=500）
            $lucky = $lucky * 2;
            // 刑伤值：输出值=x*2（若结果大于500，令其=500）
            $burn = $burn * 1.3;
            if($active > 500) $active = 500;
            if($lucky > 500) $lucky = 500;
            if($burn > 500) $burn = 500;

            $this->active[$i] = $active = round($active);
            $this->lucky[$i] = $lucky = round($lucky);
            $this->burn[$i] = $burn = round($burn);

            // 高 3  中 2  低 1
            if($active > $this->high)
            {
                $active_status = 3;
            }elseif($active > $this->mid)
            {
                $active_status = 2;
            }else
            {
                $active_status = 1;
            }

            if($lucky > $this->high)
            {
                $lucky_status = 3;
            }elseif($lucky > $this->mid)
            {
                $lucky_status = 2;
            }else
            {
                $lucky_status = 1;
            }

            if($burn > $this->high)
            {
                $burn_status = 3;
            }elseif($burn > $this->mid)
            {
                $burn_status = 2;
            }else
            {
                $burn_status = 1;
            }
            $status = $active_status.$lucky_status.$burn_status;

            $ret[] = [
                'id'=>$i,
                'name'=>$this->house_name[$i],
                'active'=>$active,
                'lucky'=>$lucky,
                'burn'=>$burn,
                'burn_status'=>$burn_status,
                'house_power_text'=>$this->getThePowerText($status),
                'house_text'=>$this->getTheHouseText($i),
                'house_status'=>$this->getTheHouseStatus($i),
            ];
        }
//        p($ret);
        return $ret;
    }

    /**
     * 计算宫位活跃值
     */
    protected function get_house_active()
    {
        $this->planet_in_house();
        $this->add_house_protect_active();
        $this->inhouse_planet_pk_outhouse_planet();
    }

    /**
     * 刑伤值：该宫位主导的事项是否遭受了破坏，破坏程度
     */
    protected function get_house_hurt()
    {
        $this->bad_planet_in_house();
        $this->house_protect_ancient_burn();
        $this->planet_in_house_burn();
    }

    /**
     * 光明值：宫位得吉的程度深浅
     */
    protected function get_house_light()
    {
        // 宫内星的吉利属性
        $this->planet_in_house_lucky();
        // 宫内星与宫外吉星的联系
        $this->inhouse_planet_pk_outhouse_lucky_planet();
        // 宫主星与吉星的联系
        $this->house_protect_pk_lucky_planet();
    }

    // 检查宫内落星
    protected function planet_in_house()
    {
        for($i=0;$i<10;$i++)
        {
            if($i == 0)
            {
                $base = 100;
                if($this->base_data['planets'][0]['in_house_id'] > 6) $base += 10;
            }
            elseif($i == 1)
            {
                $base = 100;
                if($this->base_data['planets'][0]['in_house_id'] <= 6) $base += 10;
            }
            elseif($i == 2 || $i == 3 || $i ==4)
            {
                $base = 70;
            }
            elseif($i == 5 || $i == 6)
            {
                $base = 50;
            }
            elseif($i == 7 || $i == 8 || $i == 9)
            {
                $base = 30;
            }
            $this->active_score[$this->base_data['planets'][$i]['in_house_id']] += $base;
        }
        // 命主星50（考虑现代占星双守护，若天王海王冥王是命主，天海冥加25）
        if($this->base_data['houses'][1]['protect_id'] != $this->base_data['houses'][1]['protect_ancient_id'])
        {
            $this->active_score[$this->base_data['houses'][1]['protect_inhouse_id']] += 25;
        }
        $this->active_score[$this->base_data['houses'][1]['protect_ancient_inhouse_id']] += 50;
        // 十宫主星40（考虑现代占星双守护，若天王海王冥王是十宫主，天海冥加20）
        if($this->base_data['houses'][10]['protect_id'] != $this->base_data['houses'][10]['protect_ancient_id'])
        {
            $this->active_score[$this->base_data['houses'][10]['protect_inhouse_id']] += 20;
        }
        $this->active_score[$this->base_data['houses'][10]['protect_ancient_inhouse_id']] += 40;
    }

    /**
     * 加上宫主星的活跃值
     * +宫主星的活跃值（双守护则取两个守护中的最高者）
     */
    protected function add_house_protect_active()
    {
        for($i = 1;$i < 13; $i++)
        {
            $p = $this->base_data['houses'][$i]['protect_id'];
            $ap = $this->base_data['houses'][$i]['protect_ancient_id'];
            $max = $this->base_active[$ap]['active'] *1;
            // （双守护则取两个守护中的最高者）
            if($p != $ap && $this->base_active[$p]['active'] * 1 > $this->base_active[$p]['active'] * 1 )
            {
                $max = $this->base_active['active'][$ap] * 1;
            }

            $this->active_score[$i] += $max;
        }
    }

    /**
     * 分别检测每一颗宫内星与外宫行星连结
     */
    protected function inhouse_planet_pk_outhouse_planet()
    {
        $house = $this->base_data['houses'];
        $houseadd = [];
//        p($house[1]);
        for($i=0;$i<10;$i++)
        {
            $inhouse = $this->base_data['planets'][$i]['in_house_id'];
            // 宫内星定位日或月，本宫+10分 01
            if($house[$inhouse]['protect_ancient_id'] < 2)
            {
                $this->active_score[$inhouse] += 10;
            }
            // 定位水金火，本宫+5分  234
            elseif($house[$inhouse]['protect_ancient_id'] > 1 && $house[$inhouse]['protect_ancient_id'] < 5)
            {
                $this->active_score[$inhouse] += 5;
            }
            $phase = $this->base_data['planets'][$i]['phase'];
            if(empty($phase)) continue;
//            p($phase);
            // 循环当前行星相位
            foreach($phase as $ps)
            {

                // 虚星不计算 非主相位不计算  合相不计算
                if($ps['id'] > 9 || !in_array($ps['phase_e'],[60,90,120,180])) continue;

                // 宫内星与宫外的日或月成非合相，本宫+30分（无论宫内多少星体与某颗星成相，只计一次分数） 01
                if($i < 2)
                {
                    $add = 30;
                }
                // 宫内星与宫外的水或金或火成非合相，本宫+20分（水、金、火各只计一次）234
                elseif( $i > 1 && $i < 5)
                {
                    $add = 20;
                }
                // 宫内星与宫外的木或土成非合相，本宫+15分，（木土各只计一次） 56
                elseif( $i > 4 && $i < 7)
                {
                    $add = 15;
                }
                // 宫内星与宫外的天王或海王或冥王成非合相，本宫+8分，（天海冥各只计一次） 789
                elseif( $i > 6 && $i < 10)
                {
                    $add = 8;
                }
                $pinhouse = $this->base_data['planets'][$ps['id']]['in_house_id'];// 成相星落宫
                if(!isset($houseadd[$i][$pinhouse]) && $pinhouse != $inhouse)
                {
                    $this->active_score[$pinhouse] += $add;
                    // [成相星][成相星落宫位]
                    $houseadd[$i][$pinhouse] = true;
                }
                // 若宫内星与1或10宫宫内星、ASC或MC有非合相连结，+25分（只一次），
                if(!isset($houseadd[$pinhouse.'-25']) && $pinhouse != $inhouse
                    && ($ps['id'] == ASC || $ps['id'] == MC || $pinhouse == 1 || $pinhouse == 10))
                {;
                    $this->active_score[$pinhouse] += $add;
                    // [成相星][成相星落宫位]
                    $houseadd[$pinhouse.'-25'] = true;

                }
                // 若宫内星与4或7宫宫内星有非合相连结，+20分
                if(!isset($houseadd[$pinhouse.'-20']) && $pinhouse != $inhouse && ($pinhouse == 4 || $pinhouse == 7))
                {
                    $this->active_score[$pinhouse] += 20;
                    // [成相星][成相星落宫位]
                    $houseadd[$pinhouse.'-20'] = true;
                }
            }
        }
    }

    /**
     * 宫内落凶星；（每颗宫内星计一次，相加）
     * 火星：日生，+30，夜生，+15
     * 土星：日生，+20，夜生，+40
     * 天王：+15
     * 海王：+30
     * 冥王：+25
     */
    protected function bad_planet_in_house()
    {
        // 日生盘为true  夜生盘为false
        $day = $this->base_data['planets'][0]['in_house_id'] > 6 ? true : false;
        for($i=4;$i<10;$i++)
        {
            if($i == 5) continue;
            if($i == 4)
            {
                if($day) $add = 30; else $add = 15;
            }
            elseif($i == 6)
            {
                if($day) $add = 20; else $add = 40;
            }
            elseif($i ==7)
            {
                $add = 15;
            }
            elseif($i ==8)
            {
                $add = 30;
            }
            elseif($i ==9)
            {
                $add = 25;
            }
            $p = $this->base_data['planets'][$i];
            $this->burn_score[$p['in_house_id']] += $add;
//            echo "{$this->base_data['planets'][$i]['name_whole']}落在{$p['in_house_id']}宫+$add==========>{$p['in_house_id']}宫={$this->burn_score[$p['in_house_id']]}\n";

        }
    }

    /**
     * 宫主星的刑伤（不计三王，劫夺双宫主星则取刑伤最高者）
     * 宫主星的刑伤值
     */
    protected function house_protect_ancient_burn()
    {
        for ($i=1;$i<13;$i++)
        {
            // 宫主星（古典体系）
            $pr_id = $this->base_data['houses'][$i]['protect_ancient_id'];
            // 宫主星逆行 +30
            if($this->base_data['planets'][$pr_id]['speed'] < 0)
            {

                $this->burn_score[$i] += 30;
//                echo "{$i}宫古典宫主星{$this->base_data['planets'][$pr_id]['name_whole']}逆行+30==========>{$i}宫={$this->burn_score[$i]}\n";
            }

            // 判断是否劫夺 劫夺双宫主星则取刑伤最高者
            $sign_now =$this->base_data['houses'][$i]['in_sign_id'];// 当前宫位落星座id
            if($i == 12) $sign_next =$this->base_data['houses'][1]['in_sign_id'];// 若为12宫  则下一宫位落星座id
            else $sign_next =$this->base_data['houses'][$i+1]['in_sign_id'];// 下一宫位落星座id
            if(abs($sign_next - $sign_now) == 2 || abs($sign_next - $sign_now) == 10)
            {
                if($sign_now == 1) $id1 = @config('astrolog.protect_ancient_id')[11];
                else $id1 = @config('astrolog.protect_ancient_id')[$sign_now-2];// 上一个宫位宫主星
                $id2 = @config('astrolog.protect_ancient_id')[$sign_now-1];// 当前宫位宫主星
                $pr_id = $id1;
//                if($sign_now == 12) $id2 = @config('astrolog.protect_ancient_id')[0];   edit by 20180202 17:50
                if($this->base_active[$id1]['burn'] < $this->base_active[$id2]['burn'])
                {
                    $pr_id = $id2;
                }
            }
            // 宫主星刑伤值
            $this->burn_score[$i] += $this->base_active[$pr_id]['burn'];
//            echo "{$i}宫宫主星{$this->base_data['planets'][$pr_id]['name_whole']}刑伤值+{$this->base_active['burn'][$pr_id]}==========>{$i}宫={$this->burn_score[$i]}\n";
        }

    }

    /**
     * 宫内星的刑伤
     */
    protected function planet_in_house_burn()
    {
        for($i=1;$i<13;$i++)
        {
            // 当宫内只有一颗星时，+该宫内星的刑伤值
            $in_house_area = $this->base_data['houses'][$i]['in_house_area_planet_info'];
//            p($in_house_area);
            $ids = [];
            foreach($in_house_area as $k => $iha)
            {
                if($iha['id'] < 9) $ids[] = $iha['id'];
            }
            // 宫内只有一颗星
            if(count($ids) == 1)
            {
                $this->burn_score[$i] += $this->base_active[$ids[0]]['burn'];
            }
            // 当宫内为两颗非合相星体时，+两颗星的刑伤值之合
            elseif(count($ids) == 2)
            {
                $burn1 = $this->base_active[$ids[0]]['burn'];
                $burn2 = $this->base_active[$ids[1]]['burn'];
                if(!isset($this->base_data['planets'][$ids[0]]['phase'][$ids[1]])
                    || $this->base_data['planets'][$ids[0]]['phase'][$ids[1]]['phase_e'] != 0)
                {
                    $this->burn_score[$i] += $burn1;
                    $this->burn_score[$i] += $burn2;
                }
                // 当宫内只有一对相合星体时，+刑伤最高者的刑伤值
                elseif(isset($this->base_data['planets'][$ids[0]]['phase'][$ids[1]])
                    && $this->base_data['planets'][$ids[0]]['phase'][$ids[1]]['phase_e'] == 0)
                {
                    if($burn1 > $burn2) $this->burn_score[$i] += $burn1;
                    else $this->burn_score[$i] += $burn2;
                }
            }
            elseif(count($ids) >= 3)
            {
                $n = count($ids);
                if(empty($this->base_data['structure']['asterism'])) continue;
//                p($this->base_data['structure']['asterism']);
                // 循环星群结构
                foreach($this->base_data['structure']['asterism'] as $k=> $asterism)
                {
                    foreach($asterism as $j => $aste)
                    {
                        // 紧密星群
                        if($k == 1)
                        {
                            // 只有一组紧密星群 && 同宫内
                            if(count($ids) == count($aste)
                                && $i == $aste[0]['in_house_id']
                                && $aste[0]['in_house_id'] == $aste[1]['in_house_id']
                                && $aste[1]['in_house_id'] == $aste[2]['in_house_id'])
                            {
//                            echo "{$aste['id1']}-{$aste['id2']}-{$aste['id3']}|{$n}-{$aste['num']}\n";
                                $burn1 = $this->base_active[$ids[0]]['burn'];
                                $burn2 = $this->base_active[$ids[1]]['burn'];
                                $burn3 = $this->base_active[$ids[2]]['burn'];
                                if($burn1 > $burn2 && $burn1 > $burn3) $this->burn_score[$i] += $burn1;
                                if($burn2 > $burn1 && $burn2 > $burn3) $this->burn_score[$i] += $burn2;
                                if($burn3 > $burn1 && $burn3 > $burn2) $this->burn_score[$i] += $burn3;
                            }
                            else
                            {

                            }

                        }
                        // 疏松星群 ???????????????????????????
                        elseif($k == 2)
                        {

//                        echo "{$aste['id1']}-{$asterism['id2']}-{$asterism['id3']}|{$n}-{$asterism['num']}\n";
                        }
                    }

                }
            }
        }
//        p($this->burn_score);
    }

    /**
     * 宫内星的吉利属性
     * A．	木星60，金星55，太阳30，月亮25，水星25（昼星日生，或夜星夜生，额外+10）
     * 日0木5 昼星
     * 月1金3 夜星
     * 水2  东出为昼星  西入为夜星
     * B．	5/9/10/11宫主 +10【古典体系宫主星】
     */
    protected function planet_in_house_lucky()
    {
        // 日生盘为true  夜生盘为false
        $day = $this->base_data['planets'][0]['in_house_id'] > 6 ? true : false;
        for($i=0;$i<6;$i++)
        {
            if($i == 4) continue;
            if($i == 0)
            {
                $add = 30;
                if($day) $add += 10;
            }
            elseif($i == 1)
            {
                $add = 25;
                if(!$day) $add += 10;
            }
            elseif($i == 2)
            {
                $add = 25;
                // 东出为昼星
                if($this->base_data['planets'][$i]['ancient']['sun_in_out'] == '东出')
                {
                    if($day) $add += 10;
                }
                // 西入为夜星
                else
                {
                    if(!$day) $add += 10;
                }
            }
            elseif($i == 3)
            {
                $add = 55;
                if(!$day) $add += 10;
            }
            elseif($i == 5)
            {
                $add = 60;
                if($day) $add += 10;
            }
            $this->light_score[$this->base_data['planets'][$i]['in_house_id']] += $add;
        }
        // 5/9/10/11宫主 +10【古典体系宫主星】
        $this->light_score[$this->base_data['houses'][5]['protect_ancient_inhouse_id']] += 10;
        $this->light_score[$this->base_data['houses'][9]['protect_ancient_inhouse_id']] += 10;
        $this->light_score[$this->base_data['houses'][10]['protect_ancient_inhouse_id']] += 10;
        $this->light_score[$this->base_data['houses'][11]['protect_ancient_inhouse_id']] += 10;
    }

    /**
     * 宫内星与宫外吉星的联系
     * A．三分相：木星30，金星25，太阳25，月亮20，水星10（昼星日生，或夜星夜生，额外+5）
     * B．六分相：三分相的三分之二取整
     * C．四分、对分：三分相的三分之一取整
     * D．接纳：金木+10，日月+8，水星+5
     * F．互容：有相位互容金木+15，日月+12，水星+8，无相位互容金木以接纳计分。
     */
    protected function inhouse_planet_pk_outhouse_lucky_planet()
    {
        // 日生盘为true  夜生盘为false
        $day = $this->base_data['planets'][0]['in_house_id'] > 6 ? true : false;
//        return false;
        for($i=1;$i<13;$i++)
        {
            // 宫内星
            $ihapi = $this->base_data['houses'][$i]['in_house_area_planet_info'];
//            p($ihapi);exit;
            foreach ($ihapi as $ih)
            {
                if($ih['id'] > 9) continue;// 只计算日月水金火木土天海冥[宫内星]

                if(!empty($this->base_data['planets'][$ih['id']]['phase']))
                {
                    foreach ($this->base_data['planets'][$ih['id']]['phase'] as $k => $phases)
                    {
//                        p($phases);
                        $add = 0;
                        // 非日月水金木  跳出当前循环
                        if(!in_array($phases['id'],[0,1,2,3,5]))
                        {
                            continue;
                        }
                        // 若同宫内 则跳出当前循环
                        elseif($this->base_data['planets'][$ih['id']]['in_house_id']
                            == $this->base_data['planets'][$phases['id']]['in_house_id'])
                        {
                            continue;
                        }
                        // 与太阳成相
                        elseif($phases['id'] == 0)
                        {
                            if($phases['phase_e'] == 0)
                            {
                                $add = 15;
                                if($day) $add += 5;
                            }
                            elseif($phases['phase_e'] == 60 || $phases['phase_e'] == 120)
                            {
                                $add = 25;
                                if($day) $add += 5;
                            }
                            elseif($phases['phase_e'] == 90 || $phases['phase_e'] == 180)
                            {
                                $add = 12;
                                if($day) $add += 5;
                            }
                        }
                        // 月亮
                        elseif($phases['id'] == 1)
                        {
                            if($phases['phase_e'] == 0)
                            {
                                $add = 20;
                                if(!$day) $add += 5;
                            }
                            elseif($phases['phase_e'] == 60 || $phases['phase_e'] == 120)
                            {
                                $add = 20;
                                if(!$day) $add += 5;
                            }
                            elseif($phases['phase_e'] == 90 || $phases['phase_e'] == 180)
                            {
                                $add = 10;
                                if(!$day) $add += 5;
                            }
                        }
                        // 水星
                        elseif($phases['id'] == 2)
                        {
                            if($phases['phase_e'] == 0)
                            {
                                $add = 12;
                                // 东出为昼星
                                if($this->base_data['planets'][$phases['id']]['ancient']['sun_in_out'] == '东出')
                                {
                                    if($day) $add += 5;
                                }
                                // 西入为夜星
                                else
                                {
                                    if(!$day) $add += 5;
                                }
                            }
                            elseif($phases['phase_e'] == 60 || $phases['phase_e'] == 120)
                            {
                                $add = 10;
                                // 东出为昼星
                                if($this->base_data['planets'][$phases['id']]['ancient']['sun_in_out'] == '东出')
                                {
                                    if($day) $add += 5;
                                }
                                // 西入为夜星
                                else
                                {
                                    if(!$day) $add += 5;
                                }
                            }
                            elseif($phases['phase_e'] == 90 || $phases['phase_e'] == 180)
                            {
                                $add = 5;
                                // 东出为昼星
                                if($this->base_data['planets'][$phases['id']]['ancient']['sun_in_out'] == '东出')
                                {
                                    if($day) $add += 5;
                                }
                                // 西入为夜星
                                else
                                {
                                    if(!$day) $add += 5;
                                }
                            }
                        }
                        // 金星
                        elseif($phases['id'] == 3)
                        {
                            if($phases['phase_e'] == 0)
                            {
                                $add = 30;
                                if(!$day) $add += 5;
                            }
                            elseif($phases['phase_e'] == 60 || $phases['phase_e'] == 120)
                            {
                                $add = 25;
                                if(!$day) $add += 5;
                            }
                            elseif($phases['phase_e'] == 90 || $phases['phase_e'] == 180)
                            {
                                $add = 12;
                                if(!$day) $add += 5;
                            }
                        }
                        // 木星
                        elseif($phases['id'] == 5)
                        {
                            if($phases['phase_e'] == 0)
                            {
                                $add = 35;
                                if($day) $add += 5;
                            }
                            elseif($phases['phase_e'] == 60 || $phases['phase_e'] == 120)
                            {
                                $add = 30;
                                if($day) $add += 5;
                            }
                            elseif($phases['phase_e'] == 90 || $phases['phase_e'] == 180)
                            {
                                $add = 15;
                                if($day) $add += 5;
                            }
                        }
                        $this->light_score[$i] += $add;
                    }
                }
                // 接纳status=>1 互容status=>2 互容是否有相位 phase
                foreach ($this->base_data['characteristic'] as $ci => $caa)
                {
                    if($ci != 1 && $ci != 2) continue;
                    foreach ($caa as $chara)
                    {
                        // 同宫内则跳出当前循环
                        if($this->base_data['planets'][$chara['id1']]['in_house_id']
                            == $this->base_data['planets'][$chara['id2']]['in_house_id'])
                        {
                            continue;
                        }
                        $add = 0;
                        // 接纳
                        if($chara['status'] == 1 && ($chara['id1'] == $ih['id'] || $chara['id2'] == $ih['id']))
                        {
                            if($chara['id1'] == 0 || $chara['id2'] == 0)
                            {
                                $add = 8;
                            }
                            elseif($chara['id1'] == 1 || $chara['id2'] == 1)
                            {
                                $add = 8;
                            }
                            elseif($chara['id1'] == 2 || $chara['id2'] == 2)
                            {
                                $add = 5;
                            }
                            elseif($chara['id1'] == 3 || $chara['id2'] == 3)
                            {
                                $add = 10;
                            }
                            elseif($chara['id1'] == 5 || $chara['id2'] == 5)
                            {
                                $add = 10;
                            }
                        }
                        // 互容
                        elseif($chara['status'] == 2 && ($chara['id1'] == $ih['id'] || $chara['id2'] == $ih['id']))
                        {
                            if($chara['id1'] == 0 || $chara['id2'] == 0)
                            {
                                $add = 8;
                                if($chara['phase']) $add = 12;
                            }
                            elseif($chara['id1'] == 1 || $chara['id2'] == 1)
                            {
                                $add = 8;
                                if($chara['phase']) $add = 12;
                            }
                            elseif($chara['id1'] == 2 || $chara['id2'] == 2)
                            {
                                $add = 5;
                                if($chara['phase']) $add = 8;
                            }
                            elseif($chara['id1'] == 3 || $chara['id2'] == 3)
                            {
                                $add = 10;
                                if($chara['phase']) $add = 15;
                            }
                            elseif($chara['id1'] == 5 || $chara['id2'] == 5)
                            {
                                $add = 10;
                                if($chara['phase']) $add = 15;
                            }
                        }
                        $this->light_score[$i] += $add;
                    }

                }
            }
        }
    }

    /**
     * 宫主星与吉星的联系
     * A．	合：木星35，金星30，太阳15，月亮20，水星12（昼星日生，或夜星夜生，额外+5）
     * B．	三分相：木星30，金星25，太阳25，月亮20，水星10（昼星日生，或夜星夜生，额外+5）
     * C．	六分相：三分相的三分之二取整
     * D．	四分、对分：三分相的三分之一取整
     * E．	接纳：金木+10，日月+8，水星+5
     * F．	互容：有相位互容金木+15，日月+12，水星+8，无相位互容金木以接纳计分。
     */
    protected function house_protect_pk_lucky_planet()
    {
//        return false;
//        p($this->base_data['planets'][4]['phase']);
//        p($this->base_data['characteristic']);
        $day = $this->base_data['planets'][0]['in_house_id'] > 6 ? true : false;
        for($i=1;$i<13;$i++)
        {
            // 【古典】宫主星
            $hap = $this->base_data['houses'][$i]['protect_ancient_id'];
            $add = 0;
            // 宫主星本身是太阳+20，月亮+10，木星+24，金星+18，水星+5
            if($hap == 0) $add = 20;
            elseif($hap == 1) $add = 10;
            elseif($hap == 2) $add = 5;
            elseif($hap == 3) $add = 18;
            elseif($hap == 5) $add = 24;
            $this->light_score[$i] += $add;
            if(!empty($this->base_data['planets'][$hap]['phase']))
            {
                foreach ($this->base_data['planets'][$hap]['phase'] as $k => $phases)
                {
                    $add = 0;
                    // 非日月水金木  跳出当前循环
                    if(!in_array($phases['id'],[0,1,2,3,5]))
                    {
                        continue;
                    }
                    // 与太阳成相
                    elseif($phases['id'] == 0)
                    {
                        if($phases['phase_e'] == 0)
                        {
                            $add = 15;
                            if($day) $add += 5;
                        }
                        elseif($phases['phase_e'] == 60 || $phases['phase_e'] == 120)
                        {
                            $add = 25;
                            if($day) $add += 5;
                        }
                        elseif($phases['phase_e'] == 90 || $phases['phase_e'] == 180)
                        {
                            $add = 12;
                            if($day) $add += 5;
                        }
                    }
                    // 月亮
                    elseif($phases['id'] == 1)
                    {
                        if($phases['phase_e'] == 0)
                        {
                            $add = 20;
                            if(!$day) $add += 5;
                        }
                        elseif($phases['phase_e'] == 60 || $phases['phase_e'] == 120)
                        {
                            $add = 20;
                            if(!$day) $add += 5;
                        }
                        elseif($phases['phase_e'] == 90 || $phases['phase_e'] == 180)
                        {
                            $add = 10;
                            if(!$day) $add += 5;
                        }
                    }
                    // 水星
                    elseif($phases['id'] == 2)
                    {
                        if($phases['phase_e'] == 0)
                        {
                            $add = 12;
                            // 东出为昼星
                            if($this->base_data['planets'][$phases['id']]['ancient']['sun_in_out'] == '东出')
                            {
                                if($day) $add += 5;
                            }
                            // 西入为夜星
                            else
                            {
                                if(!$day) $add += 5;
                            }
                        }
                        elseif($phases['phase_e'] == 60 || $phases['phase_e'] == 120)
                        {
                            $add = 10;
                            // 东出为昼星
                            if($this->base_data['planets'][$phases['id']]['ancient']['sun_in_out'] == '东出')
                            {
                                if($day) $add += 5;
                            }
                            // 西入为夜星
                            else
                            {
                                if(!$day) $add += 5;
                            }
                        }
                        elseif($phases['phase_e'] == 90 || $phases['phase_e'] == 180)
                        {
                            $add = 5;
                            // 东出为昼星
                            if($this->base_data['planets'][$phases['id']]['ancient']['sun_in_out'] == '东出')
                            {
                                if($day) $add += 5;
                            }
                            // 西入为夜星
                            else
                            {
                                if(!$day) $add += 5;
                            }
                        }
                    }
                    // 金星
                    elseif($phases['id'] == 3)
                    {
                        if($phases['phase_e'] == 0)
                        {
                            $add = 30;
                            if(!$day) $add += 5;
                        }
                        elseif($phases['phase_e'] == 60 || $phases['phase_e'] == 120)
                        {
                            $add = 25;
                            if(!$day) $add += 5;
                        }
                        elseif($phases['phase_e'] == 90 || $phases['phase_e'] == 180)
                        {
                            $add = 12;
                            if(!$day) $add += 5;
                        }
                    }
                    // 木星
                    elseif($phases['id'] == 5)
                    {
                        if($phases['phase_e'] == 0)
                        {
                            $add = 35;
                            if($day) $add += 5;
                        }
                        elseif($phases['phase_e'] == 60 || $phases['phase_e'] == 120)
                        {
                            $add = 30;
                            if($day) $add += 5;
                        }
                        elseif($phases['phase_e'] == 90 || $phases['phase_e'] == 180)
                        {
                            $add = 15;
                            if($day) $add += 5;
                        }
                    }
                    $this->light_score[$i] += $add;
                }
            }
            // 接纳status=>1 互容status=>2 互容是否有相位 phase
            foreach ($this->base_data['characteristic'] as $ci => $caa)
            {
                $add = 0;
                if($ci != 1 && $ci != 2) continue;
                foreach ($caa as $chara)
                {
// 接纳
                    if($chara['status'] == 1 && ($chara['id1'] == $hap || $chara['id2'] == $hap))
                    {
                        if($chara['id1'] == 0 || $chara['id2'] == 0)
                        {
                            $add = 8;
                        }
                        elseif($chara['id1'] == 1 || $chara['id2'] == 1)
                        {
                            $add = 8;
                        }
                        elseif($chara['id1'] == 2 || $chara['id2'] == 2)
                        {
                            $add = 5;
                        }
                        elseif($chara['id1'] == 3 || $chara['id2'] == 3)
                        {
                            $add = 10;
                        }
                        elseif($chara['id1'] == 5 || $chara['id2'] == 5)
                        {
                            $add = 10;
                        }
                    }
                    // 互容
                    elseif($chara['status'] == 2 && ($chara['id1'] == $hap || $chara['id2'] == $hap))
                    {
                        if($chara['id1'] == 0 || $chara['id2'] == 0)
                        {
                            $add = 8;
                            if($chara['phase']) $add = 12;
                        }
                        elseif($chara['id1'] == 1 || $chara['id2'] == 1)
                        {
                            $add = 8;
                            if($chara['phase']) $add = 12;
                        }
                        elseif($chara['id1'] == 2 || $chara['id2'] == 2)
                        {
                            $add = 5;
                            if($chara['phase']) $add = 8;
                        }
                        elseif($chara['id1'] == 3 || $chara['id2'] == 3)
                        {
                            $add = 10;
                            if($chara['phase']) $add = 15;
                        }
                        elseif($chara['id1'] == 5 || $chara['id2'] == 5)
                        {
                            $add = 10;
                            if($chara['phase']) $add = 15;
                        }
                    }
                    $this->light_score[$i] += $add;
                }
            }
        }
    }



    /**
     * @param string $str '333'即高高高 '222'中中中 '111'低低低
     * @return array
     */
    protected function getThePowerText($str = '111')
    {
        $house_status = str_split($str);
        switch ($house_status[0])
        {
            case '1':
                $active_name = '不太活跃';
                $active_text = '该宫事务可能不是你人生的重点，不过十二个宫位主导人生十二个面向，其实每一个都不可或缺，都很重要。';
                break;
            case '2':
                $active_name = '中度活跃';
                $active_text = '一般越是活跃的宫位对你来说就越是重要，越能影响你的工作、交际和人生环境。';
                break;
            case '3':
                $active_name = '非常活跃';
                $active_text = '此宫位高度活跃，你往往会不由自主地开展这个领域的事务，它会是你生命中的核心之一。';
                break;
        }
        switch ($house_status[1])
        {
            case '1':
                $lucky_name = '成就很低';
                $lucky_text = '这一宫的人生领域很难是你成功的方向。';
                break;
            case '2':
                $lucky_name = '成就中等';
                $lucky_text = '一个宫位的成就越高，它所指向的领域就越容易成为你成功的方向，抑或给你带来诸多好处。';
                break;
            case '3':
                $lucky_name = '成就非凡';
                $lucky_text = '一个成就很高的宫位，即便它无法赐予你真正的成功，也一定能让你在此领域内屡有斩获。';
                break;
        }
        switch ($house_status[2])
        {
            case '1':
                $burn_name = '没啥挑战';
                $burn_text = '你不会在这里面对太多的困难，如果这一宫同时也有较高的成就值，你往往会在此受益匪浅。';
                break;
            case '2':
                $burn_name = '有些挑战';
                $burn_text = '挑战越高意味着需要克服越多困难，而随之而来的收获也会越大，同时挑战越高也越是不容逃避。';
                break;
            case '3':
                $burn_name = '挑战极大';
                $burn_text = '这里困难重重，但都是你必须要面对的东西，迎接挑战，打破困境，逐步成长，这是你注定要走的道路。';
                break;
        }

        $data = [[$active_name,$active_text],[$lucky_name,$lucky_text],[$burn_name,$burn_text]];

        return $data;
    }


    protected function getTheHouseText($id)
    {
        $house_area = [
            1=>'人格、外形、个人表现、身体',
            2=>'金钱、物质、价值观、价值实现',
            3=>'说、写、信息交流、基本教育、短线出行、兄弟姐妹',
            4=>'家庭、父亲、不动产',
            5=>'游戏、娱乐、子女、创造、投资、赌博、戏剧、表现',
            6=>'工作、日常琐事、豢养宠物、健康',
            7=>'婚姻、合作、关系、公开的敌人、法律纠纷',
            8=>'遗产、税务、股票等借由他人所得的钱，生死、危机、性、神秘学',
            9=>'出国、旅行、高等教育、宗教、法律、哲学、宗教、信仰',
            10=>'事业、名声、地位、上司、父亲或母亲',
            11=>'朋友、同道、团体、政治',
            12=>'慈善、出世、潜在的力量、隐藏的敌人',
        ];
        $leading = $house_area[$id];

        return ['leading'=>$leading];
    }

    /**
     * 获取宫位状态   合轴|至高|繁忙|福星|煞星|中点|明耀|紧张|激战|得时|空相|天佑|陷阱|辅佐|独明
     * @param $k 宫位id
     * @return array
     */
    protected function getTheHouseStatus($id)
    {
        $data = [
            'hebi' => ['fouth', '合璧', '太阳月亮的光芒在此汇聚，你在这里拥有天赋和好运'],
            'xiangong' => ['first', '仙宫', '吉星云集，这里有你逃不开的运气和机缘'],
            'modian' => ['third', '魔殿', '凶星汇聚，即便你在此拥有过人的能力，也可能付出多收获少'],
            'qingchun' => ['first', '清纯', '这里的能量纯净而强大，你很容易从中获益'],
            'panxin' => ['fouth', '盘心', '这里是你的生命核心，生活的重中之重，有着牵扯不断的缘分'],
            'anjing' => ['fouth', '安静', '此处不会有太多故事，也少有事故发生'],
            'zhanchang' => ['third', '战场', '这里有激烈的挑战在等着你，打破困局，你会获得新生'],
            'huian' => ['twice', '晦暗', '往往会有些不为人知或不被人理解的事件，伴随厄运和纷争'],
            'wuguang' => ['twice', '无光', '不为人知或不被理解，这里很可能有一大堆的秘密，有关现实的厄运和心灵的迷惑，你不得不自我消化，学会成长。'],
            'shaqi' => ['third', '杀气', '生活迟早会在这里留下一道伤疤，也可能是两道、三道。'],
            'xingdu' => ['third', '刑毒', '你不断在这个领域遭遇坎坷，只是为了领悟隐藏在此的人生道理，彻底掌控这个领域'],
            'hundun' => ['twice', '混沌', '这个领域对你而言非常重要，同时也是吉凶混杂，难以分辨'],
            'foyan' => ['first', '佛眼', '清心寡欲、安静修行或许会让你活得更好'],
        ];

        $ret = [];
        // 合璧
        if ($this->is_hebi($id)) $ret[] = $data['hebi'];

        return [];
    }

    private function is_hebi($id)
    {
        $planets = $this->base_data['planets'];
        // 排除日月刑冲的状况
        if(!isset($planets[0]['phase']['0-1']) || in_array($planets[0]['phase']['0-1']['phase_e'],[90,180]))
        {

        }
        return false;
    }
    private function is_xiangong($id)
    {
        return false;
    }
    private function is_modian($id)
    {
        return false;
    }
    private function is_qingchun($id)
    {
        return false;
    }
    private function is_panxin($id)
    {
        return false;
    }
    private function is_anjing($id)
    {
        return false;
    }
    private function is_zhanchang($id)
    {

        return false;
    }
    private function is_huian($id)
    {

        return false;
    }
    private function is_wuguang($id)
    {

        return false;
    }
    private function is_shaqi($id)
    {

        return false;
    }
    private function is_xingdu($id)
    {

        return false;
    }
    private function is_hundun($id)
    {

        return false;
    }
    private function is_foyan($id)
    {

        return false;
    }

}